﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SignalR.Hubs;
using System.Threading.Tasks;
using EvincePayroll.Data;

namespace EvinceUI
{
    public class ChatHub:Hub
    {
        public void Send(string senderNameAndId, string message, string userName, string targetUserId)
        {
            //if (!string.IsNullOrWhiteSpace(group))
            //{
            //    Clients[group].addMessage(message);
            //}
            string clientId = senderNameAndId.Split('_')[0];
            if (string.IsNullOrWhiteSpace(clientId))
            {
                Clients.addMessage(senderNameAndId,message, userName);
            }
            else if (!string.IsNullOrWhiteSpace(clientId))
            {
                Caller.addMessage(senderNameAndId, message, userName);
                Clients[clientId].addMessage(targetUserId, message, userName, targetUserId);
            }
        }

        public Task Join(string groupName, string userName)
        {
            Clients[groupName].addUsers(userName).Wait();
            return this.Groups.Add(Context.ConnectionId, groupName);
        }

        public void GoOnline(string employeeId)
        {
           // ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
          //  var employee = client.GetEmployeeDataByEmployeeId(employeeId);
         //   Clients.goOnlineMessage(employeeId, employee.FirstName + " " + employee.LastName);
            Clients.goOnlineMessage(employeeId);
        }


        public Task Disconnect()
        {
            return Clients.leave(Context.ConnectionId, DateTime.Now.ToString());
        }
    }
}